//
//  TVCommodityDetailSpecCell.swift
//  iPhone
//
//  Created by Toj on 8/15/22.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class TVCommodityDetailSpecCell: UICollectionViewCell {
    
    var cellModel: TVCommodityCellModel? { didSet {
        guard let model = cellModel else { return }
        
        priceLabel.attributedText = model.priceString
        invalidPriceLabel.attributedText = model.listPriceString
        
        // 时间
        dateView.setTitle(model.name, for: .normal)
        
        let imgName = model.isSelected ? "commodity_border_sel" : "commodity_border_normal"
        backImageView.image = imgName.to_image
        
        giftImageview.isHidden = !model.isDiscount
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    // 选择边框View
    private lazy var backImageView: UIImageView = UIImageView()
    
    // 时间
    private lazy var dateView: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.titleLabel?.font = 16.to_rbtBoldFont
        view.setTitleColor("#99773C".to_color, for: .normal)
        return view
    }()
  
    // 礼物图标
    private lazy var giftImageview: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named:"commodity_gift")
        return view
    }()
    
    // 现价格
    private lazy var priceLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#554422".to_color
        return view
    }()

    // 原价
    private lazy var invalidPriceLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#AAAAAA".to_color
        view.font = UIFont.systemFont(ofSize: 14)
        view.textAlignment = .center
        return view
    }()
}

// MARK: - 初始化
extension TVCommodityDetailSpecCell {
    
    private func setupMakeAddSubviews() {
     
        contentView.addSubview(dateView)
        contentView.addSubview(backImageView)
        contentView.addSubview(giftImageview)
        contentView.addSubview(priceLabel)
        contentView.addSubview(invalidPriceLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        backImageView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        dateView.snp.makeConstraints { make in
            make.top.left.right.equalTo(contentView)
            make.height.equalTo(42)
        }
        
        giftImageview.snp.makeConstraints { make in
            make.left.equalTo(2)
            make.top.equalTo(2)
            make.width.equalTo(14)
            make.height.equalTo(14)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.top.equalTo(dateView.snp.bottom)
            make.left.right.equalTo(contentView)
            make.height.equalTo(24)
        }
        
        invalidPriceLabel.snp.makeConstraints { make in
            make.top.equalTo(priceLabel.snp.bottom).offset(4)
            make.left.right.equalTo(contentView)
            make.height.equalTo(14)
        }
    }
}
